﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Collections;
using System.Text;
using Portal.Environment;
using Portal.Language;
using Portal.Interface;

namespace Portal.CDSP
{
    public class AlbumDetailWP : WebPart
    {
        public override string WebPartId
        {
            get
            {
                return "WebSite.AlbumDetailWP";
            }
        }

        public override string WebPartFolder
        {
            get
            {
                return "Portal.CDSP";
            }
        }


        public override string Title
        {
            get
            {
                return GetText("Title", "CDSP-Xem chi tiết album ảnh");
            }
        }

        public override ViewCls[] Views
        {
            get
            {
                return new ViewCls[]{
                    new ViewCls("default",GetText("default","Kiểu ngầm định"),1),
                };
            }
        }



        public override string NameSpace
        {
            get
            {
                return "Portal.CDSP";
            }
        }

        public override string ClassName
        {
            get
            {
                return "AlbumDetailWP";
            }
        }


        private string ClientRenderWebPart(string TabModuleId, string HasPorlet)
        {
            TabModuleCls TabModuleCls = new TabModuleBLL().CreateModelFromXml(TabModuleId);
            if (TabModuleCls == null) throw new Exception(GetText("TabModuleIdNull", "TabModuleId is null! Csdl sai"));
            string AlbumId = (string)Environments.Request("AlbumId");
            StringBuilder RetHtml = new StringBuilder();
            RetHtml.Append(
                WebPartUtilities.AttachJavascript(WebPartFolder, "FDetailsPhoto.js"));
            if (AlbumId == null) AlbumId = "";
            if (AlbumId.Trim().Length == 0)
            {
                return "<div class=\"clrfix\"></div>" +
                       "<div class=cssAccessDenided>" + GetText("AccessDenided", "Yêu cầu truy cập không hợp lệ!") + "</div>";
            }
            RetHtml.Append(
                    " <div class=\"PageTitleBar\">\n" +
                    "	<ul class=\"breadcrumb\">\n" +
                    "	    <li class=\"breadcrumblv1\"><a href=\"AlbumList.aspx\">Thư viện ảnh</a></li>\n" +
                    "		<li class=\"breadcrumblv2\">Xem chi tiết album ảnh</li>\n" +
                    "	</ul>\n" +
                    "	<div class=\"clr\"></div>\n" +
                    " </div>\n" +

                    "<div class=\"ArticleDetail\">" +
                        GetHtml(AlbumId, 20, 10, 0, "JumpPageAlbumListDetails") +
                    "</div>");
               
            return RetHtml.ToString();
        }

        [AjaxPro.AjaxMethod]
        public string GetHtml(string AlbumId, int ItemsPerRow, int NoOfVisiblePage, int CurrentPage, string JumpJavaScript)
        {
            StringBuilder RetHtml = new StringBuilder();
            try
            {                
                DataTable dtItems = new FAlbumBLL().ReadingImagesByAlbumId(AlbumId);
             
                if (dtItems.Rows.Count == 0)
                {
                    return "<div class=\"clrfix\"></div>" +
                           "<div class=cssNoImageForVideo>" + GetText("NoImage1", "Không có ảnh nào trong album. ") + "</div>";
                }

                int StartIndex = 0;
                int EndIndex = 0;
                string PagingText = new CDSPPaging().GetPaging(AlbumId, "vie", dtItems.Rows.Count, ItemsPerRow, NoOfVisiblePage, CurrentPage, JumpJavaScript, ref StartIndex, ref EndIndex);               
                
                string AlbumName = (string)dtItems.Rows[0]["vieSubject"];
                RetHtml.Append(
                 "			<h3 class=\"ArticleTittle\">" + AlbumName + "</h3>\n" +
                     "<ul class=\"listnews\">");
                for (int iIndex = StartIndex; iIndex < EndIndex; iIndex++)
                {
                    string AdvImageId = (string)dtItems.Rows[iIndex]["fk_AdvImage_Id"];
                    string AdvImageItemName = "";
                    try
                    {
                        AdvImageItemName = (string)dtItems.Rows[iIndex]["AdvImageItemName"];
                    }
                    catch { AdvImageItemName = ""; }
                    string belongSite = (string)dtItems.Rows[iIndex]["belongSite"];
                    string Description = "";
                    try
                    {
                        Description = (string)dtItems.Rows[iIndex]["Description"];
                    }
                    catch { Description = ""; }

                    string TotalClicked = ((int)dtItems.Rows[iIndex]["TotalClicked"]).ToString();

                    string EntryDate = ((DateTime)dtItems.Rows[iIndex]["ItemEntryDate"]).ToString();

                    string AdvImageItemId = (string)dtItems.Rows[iIndex]["AdvImageItemId"];
                    string FileImage = (string)dtItems.Rows[iIndex]["FileImage"];
                    string ImageUrl = Environments.HttpMediaRoot + "/Media/" + Environments.PortalAlias + "/AdvImage/Images/" + belongSite + "/" + AdvImageId + "/" + FileImage;
                    RetHtml.Append(
                            " <li>" +
                                " <div class=\"photoholder\">" +
                                "    <a rel=\"prettyPhoto[pp_gal]\" title=\"" + Description + "\" href=\"" + ImageUrl + "\">" +
                                "    <img src=\"" + ImageUrl + "\" alt=\"" + AlbumName + "\" class=\"photothum\"/>" +
                                "    <span class=\"newstitle\">" + AdvImageItemName + "</span></a>" +
                                "    <span>" + Description + "</span>" +
                                " </div>" +
                            " </li>" +
                            " <div class=colfix></div>");
                }
                RetHtml.Append(
                    "</ul>"+
                    "<div class=\"colfix\"></div>" +
                    PagingText);               
            }
            catch (Exception ex)
            {
                 RetHtml.Append("ERROR:" + ex.ToString());
            }
            return RetHtml.ToString();
        }


        public string GetViewAlbum(string belongSite, string Subject, string ShortId, bool UseGallery, string PageIndex)
        {
            Subject = Environments.ConvertSubject(Subject);

            string Url = "";
            if (UseGallery == false)
            {
                Url = Environments.FrontHttpRoot + "/" + Environments.AppendAlias + "-" + belongSite + "/" + Subject + "-va-" + ShortId + "-page-" + PageIndex + "." + Environments.Extension;
                if (PageIndex.Equals("0"))
                {
                    Url = Environments.FrontHttpRoot + "/" + Environments.AppendAlias + "-" + belongSite + "/" + Subject + "-va-" + ShortId + "." + Environments.Extension;
                }
            }
            else
            {
                Url = Environments.FrontHttpRoot + "/" + Environments.AppendAlias + "-" + belongSite + "/" + Subject + "-vas-" + ShortId + "-page-" + PageIndex + "." + Environments.Extension;
                if (PageIndex.Equals("0"))
                {
                    Url = Environments.FrontHttpRoot + "/" + Environments.AppendAlias + "-" + belongSite + "/" + Subject + "-vas-" + ShortId + "." + Environments.Extension;
                }
            }
            if (Environments.MultiLanguageInSite == 1)
            {
                Url = Url + "?lang=" + Environments.ActiveLanguage;
            }
            return Url;
        }

        [AjaxPro.AjaxMethod]
        public string GetNextUrl(string belongSite, string AlbumId, int CurrentPageIndex)
        {
            return GetViewAlbum(belongSite, "xem-anh", AlbumId, true, CurrentPageIndex.ToString());
        }

        [AjaxPro.AjaxMethod]
        public override string ClientRenderWebPart(string BelongSite, string TabModuleId, string ViewName, string HasPorlet, LoadParamCls loadParamCls)
        {
            try
            {
                if (ViewName.Equals("default"))
                {
                    return ClientRenderWebPart(TabModuleId, HasPorlet);
                }


                return GetText("ViewNotFound", "Không tìm thấy View [" + ViewName + "]");
            }
            catch (Exception ex)
            {
                return ex.Message.ToString();
            }
        }


        public override DataTable ReadingDataService(string belongSite, string ActiveLanguage, ref string IdField, ref string SubjectField)
        {
            IdField = "Id";
            SubjectField = "Subject";
            DataTable dtResult = new DataTable();
            dtResult.Columns.Add("Id");
            dtResult.Columns.Add("Subject");
            dtResult.Rows.Add(new object[]{
                "FDF3D60E-2A4E-44d2-81B9-149025F1E365","Chi tiết album ảnh"
            });
            return dtResult;
        }

        public override bool AllowFrontService
        {
            get
            {
                return true;
            }
        }
    }
}
